package cn.wolfcode.web.controller;

import cn.wolfcode.domain.*;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;


    @RequestMapping("/list")
    @RequiresPermissions(value = {"role:list","角色列表"},logical = Logical.OR)
    public String list(Model model,QueryObject qo){
        PageInfo<Role> pageResult = roleService.query(qo);
        model.addAttribute("pageInfo", pageResult);
        return "role/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"role:delete","角色删除"},logical = Logical.OR)
    public String delete(Long id){
        roleService.deleteByPrimaryKey(id);
        return "redirect:/role/list.do";
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"role:input","角色编辑"},logical = Logical.OR)
    public String input(Long id,Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions", permissions);
        if(id != null){
            Role role = roleService.selectByPrimaryKey(id);
            model.addAttribute("role", role);
        }
        return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"role:saveOrUpdate","角色新增或修改"},logical = Logical.OR)
    public String saveOrUpdate(Role role,Long[] ids){
        if(role.getId() != null){
            roleService.updateByPrimaryKey(role,ids);
        }else {
            roleService.insert(role,ids);
        }
        return "redirect:/role/list.do";
    }
}
